package cn.yunhe.action;

import cn.yunhe.biz.NewsBizDao;
import cn.yunhe.entity.News;
import cn.yunhe.entity.Page;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import javax.annotation.Resource;
import java.util.Map;

public class NewsAction extends ActionSupport implements ModelDriven {
    @Resource
    private NewsBizDao newsBizDao;

    private Page page;


    public String searchNews() throws Exception {
        page =newsBizDao.searchNews(page);
        return SUCCESS;
    }

    public String delNews(){
        newsBizDao.delNewById(page.getNews().getNewId());
        return "input";
    }


    public String addNews(){
        newsBizDao.addNews(page.getNews());
        return "input";
    }

   public String getNews(){
    News news = newsBizDao.getNewsById(page.getNews().getNewId());
      Map map = (Map) ActionContext.getContext().get("request");
      map.put("news",news);
      return "news";
   }

   public String updateNews(){
        newsBizDao.updateNews(page.getNews());
       return "input";
   }
    @Override
    public Object getModel() {
       if (page==null){
           page=new Page();
       }
       return page;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public NewsBizDao getNewsBizDao() {
        return newsBizDao;
    }

    public void setNewsBizDao(NewsBizDao newsBizDao) {
        this.newsBizDao = newsBizDao;
    }
}
